﻿#include "nuf_bus_info.h"
#include "com_manager.h"

#include <QDir>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QApplication>
#include <QVBoxLayout>
#include <QLabel>
#include <QComboBox>

QList<_NUF_BUS> NufBusSetting::map_bus;

NufBusSetting::NufBusSetting()
{

}

bool NufBusSetting::loadSetting()
{


    if ( load_board_setting() == false)
    {
        init_board_default(); //initial default
    }

    save_Bus_Setting();
    return false;
}

void NufBusSetting::init_board_default()
{
    if (com_manager::instance()->comList.count()==0)
        return;

    _NUF_BUS bus_0;
    bus_0.port_name = com_manager::instance()->curComName;//comList.first();
    for (int i = 0; i < 12; i++)
    {
        QString  id = QString("ethernet_%1").arg(  i + 1,2, 10, QLatin1Char('0'));
        bus_0.p_list .append( id );
    }
    map_bus.append(bus_0);

    _NUF_BUS bus_1;
    bus_1.port_name = com_manager::instance()->curComName;//comList.first();
    for (int i = 12; i < 24; i++)
    {
        QString  id = QString("ethernet_%1").arg(  i + 1,2, 10, QLatin1Char('0'));
        bus_1.p_list <<id ;
    }
    map_bus.append(bus_1);

    for ( int i =0; i < 10; i ++ )
    {
        _NUF_BUS bus ;
        bus.port_name = com_manager::instance()->curComName;//comList.first();
        bus .p_list <<QString("ethernet_%1").arg(  24 +i*2 + 1,2, 10, QLatin1Char('0'))  ;
        bus .p_list <<QString("ethernet_%1").arg(  24 +i*2 + 2,2, 10, QLatin1Char('0')) ;

        bus .p_list <<  QString("can_%1").arg(  i *4+ 1, 2,10, QLatin1Char('0'));
        bus .p_list <<  QString("can_%1").arg(  i *4+ 2, 2,10, QLatin1Char('0'));
        // bus .p_list <<  QString("can_%1").arg(  i *4+ 3, 2,10, QLatin1Char('0'));
        // bus .p_list <<  QString("can_%1").arg(  i *4+ 4, 2,10, QLatin1Char('0'));

        bus .p_list <<  QString("485_%1").arg(  i *4+ 1, 2,10, QLatin1Char('0'));
        bus .p_list <<  QString("485_%1").arg(  i *4+ 2, 2,10, QLatin1Char('0'));
        // bus .p_list <<  QString("485_%1").arg(  i *4+ 3, 2,10, QLatin1Char('0'));
        // bus .p_list <<  QString("485_%1").arg(  i *4+ 4, 2,10, QLatin1Char('0'));

        map_bus.append(bus );
    }

}

bool NufBusSetting::load_board_setting()
{
    QFile fileArray(QApplication::applicationDirPath() + "/data/board_setting.json");
    if (fileArray.open(QIODevice::ReadOnly))
    {
        map_bus.clear();

        QByteArray bytes = fileArray.readAll();
        QJsonDocument doc = QJsonDocument::fromJson(bytes);

        QJsonObject all = doc.object();

        QStringList keys = all.keys();
        int cnt_485 = all.keys().count();
        int i = 1;
        foreach (QString key, keys)
        {
            QJsonObject  obj = all.value(key).toObject();

            QString id = QString("board_%1").arg(  i++ + 1,2, 10, QLatin1Char('0'));
            _NUF_BUS  bus;
            bus.port_name = obj["port_name"].toString();
            bus.mac_id = obj["mac_addr"].toString();
            QJsonArray p =obj["p"].toArray() ;
            for  (int i =0; i < p.count(); i++ ) {
                bus.p_list.append(p.at(i).toString());
            }

            map_bus.append( bus);
        }
        return true;
    }
    return false;
}

void NufBusSetting::save_Bus_Setting()
{
    QDir dir(qApp->applicationDirPath());
    if (!dir.exists("data"))
        dir.mkpath("data");

    //QJsonArray arrayNet;
    QJsonObject arrayMap;

    int i = 1;

    foreach (_NUF_BUS bus, map_bus) {

        QJsonArray arrayBus;
        foreach (QString node_id,bus.p_list ) {
            arrayBus.append(node_id);
        }

        QJsonObject obj;
        obj["port_name"] = bus.port_name;

        obj["mac_addr"] = bus. mac_id ;
        obj["p"] = arrayBus;

        arrayMap[QString("board_%1").arg(i++, 2, 10, QLatin1Char('0'))] = obj;
    }


    QByteArray bytes = QJsonDocument(arrayMap).toJson();

    QFile fileArray(QApplication::applicationDirPath() + "/data/board_setting.json");
    if (fileArray.open(QIODevice::WriteOnly))
    {
        fileArray.write(bytes);
        fileArray.close();
    }
}
